package com.zksk.admin.controller.invoice;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zksk.admin.domain.invoice.dto.InvoiceDto;
import com.zksk.admin.domain.invoice.dto.InvoiceRequestDto;
import com.zksk.admin.domain.invoice.vo.InvoiceRequestVo;
import com.zksk.admin.domain.member.vo.MemberVo;
import com.zksk.admin.domain.orders.dto.OrdersDto;
import com.zksk.admin.service.invoice.InvoiceService;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.common.security.utils.SecurityUtils;
import com.zksk.mbg.admin.domain.MemberInvoiceRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 发票类 信息操作处理
 *
 * @author zksk
 */
@Slf4j
@RestController
@RequestMapping("/invoice")
@Api(value = "InvoiceController", tags = "发票相关接口")
public class InvoiceController extends BaseController {

    @Autowired
    private InvoiceService invoiceService;

    /**
     *
     * 获取发票列表
     */
    @Log(title = "财务管理-发票列表", businessType = BusinessType.SELECT)
    @ApiOperation(value = "获取订单列表")
    @PostMapping("/list")
    public R<IPage<InvoiceDto>> list(@RequestBody InvoiceRequestVo invoiceRequestVo){
        IPage<InvoiceDto> resultPage = invoiceService.invoiceList(invoiceRequestVo);
        return R.ok(resultPage, "获取发票列表数据成功");
    }

    @GetMapping("/getById/{id}")
    @Log(title = "查询发票信息", businessType = BusinessType.SELECT)
    @ApiOperation(value = "根据发票记录ID查询发票信息")
    public R<InvoiceRequestDto> getById(@PathVariable Long id)
    {
        return invoiceService.selectById(id);
    }

    @PutMapping("/update")
    @Log(title = "修改用户开票记录信息", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改用户开票记录信息")
    public R update(@RequestBody MemberInvoiceRecord memberInvoiceRecord){
        memberInvoiceRecord.setUpdateBy(SecurityUtils.getUsername());
        invoiceService.saveOrUpdate(memberInvoiceRecord);
        return R.ok(memberInvoiceRecord,"修改成功");
    }

    @Log(title = "根据订单id查询开票记录信息",businessType = BusinessType.SELECT)
    @GetMapping("/{ordersId}")
    @ApiOperation(value = "根据订单id查询开票记录信息")
    public R<MemberInvoiceRecord> getByOrdersId(@PathVariable Long ordersId){
        QueryWrapper<MemberInvoiceRecord> recordQueryWrapper = new QueryWrapper<MemberInvoiceRecord>()
                .eq("orders_id",ordersId);
        MemberInvoiceRecord memberInvoiceRecord = invoiceService.getOne(recordQueryWrapper);
        return R.ok(memberInvoiceRecord,"查询开票记录信息成功");
    }

    @Log(title = "保存开票记录信息",businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "保存开票记录信息")
    public R<MemberInvoiceRecord> saveInvoiceRecord(@RequestBody MemberInvoiceRecord memberInvoiceRecord){
        memberInvoiceRecord.setCreateBy(SecurityUtils.getUsername());
        invoiceService.saveOrUpdate(memberInvoiceRecord);
        return R.ok(memberInvoiceRecord,"保存开票记录信息成功");
    }


}
